package com.co.user.service.impl;

import com.co.common.entity.Result;
import com.co.common.util.IdWorker;
import com.co.user.pojo.History;
import com.co.user.mapper.HistoryMapper;
import com.co.user.pojo.Like;
import com.co.user.service.HistoryService;
import com.co.video.feign.DetailFeign;
import com.co.video.feign.VideoFeign;
import com.co.video.pojo.Detail;
import com.co.video.pojo.Video;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 * UsHistory业务层接口实现类
 */
@Service
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    private HistoryMapper historyMapper;

    @Autowired
    private DetailFeign detailFeign;

    @Autowired
    private IdWorker idWorker;


    /**
     * UsHistory条件+分页查询
     *
     * @param history 查询条件
     * @param page    页码
     * @param size    页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<History> findPage(History history, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(history);
        //执行搜索
        return new PageInfo<History>(historyMapper.selectByExample(example));
    }

    /**
     * UsHistory分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<History> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<History>(historyMapper.selectAll());
    }

    /**
     * UsHistory条件查询
     *
     * @param history
     * @return
     */
    @Override
    public List<History> findList(History history) {
        //构建查询条件
        Example example = createExample(history);
        //根据构建的条件查询数据
        return historyMapper.selectByExample(example);
    }


    /**
     * UsHistory构建查询对象
     *
     * @param history
     * @return
     */
    public Example createExample(History history) {
        Example example = new Example(History.class);
        Example.Criteria criteria = example.createCriteria();
        if (history != null) {
            // 主键
            if (!StringUtils.isEmpty(history.getId())) {
                criteria.andEqualTo("id", history.getId());
            }
            // 用户表主键
            if (!StringUtils.isEmpty(history.getUsername())) {
                criteria.andLike("username", "%" + history.getUsername() + "%");
            }
            // vdetail的主键
            if (!StringUtils.isEmpty(history.getAid())) {
                criteria.andEqualTo("aid", history.getAid());
            }
            // 创建时间
            if (!StringUtils.isEmpty(history.getCreateTime())) {
                criteria.andEqualTo("createTime", history.getCreateTime());
            }
            // 修改时间
            if (!StringUtils.isEmpty(history.getUpdateTime())) {
                criteria.andEqualTo("updateTime", history.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        historyMapper.deleteByPrimaryKey(id);
    }

    /**
     * 删除所有历史记录
     */
    @Override
    public void deleteAll() {
        historyMapper.deleteAll();
    }

    /**
     * 修改UsHistory
     *
     * @param history
     */
    @Override
    public void update(History history) {
        historyMapper.updateByPrimaryKey(history);
    }

    /**
     * 增加UsHistory
     *
     * @param history
     */
    @Override
    public void add(History history) {
        //获取用户名
        String username = history.getUsername();
        //获取视频的aid
        String aid = history.getAid();

        History historyData = historyMapper.selectOne(history);

        if(historyData==null){
            history.setCreateTime(new Date());
            history.setId(String.valueOf(idWorker.nextId()));
            historyMapper.insertSelective(history);
            return;
        }
        if (aid.equals(historyData.getAid())){
            String id = historyData.getId();
            System.out.println(id);
            history.setCreateTime(new Date());
            history.setId(historyData.getId());
        }
        historyMapper.updateByPrimaryKey(history);


    }

    /**
     * 根据ID查询UsHistory
     *
     * @param id
     * @return
     */
    @Override
    public History findById(String id) {
        return historyMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询UsHistory全部数据
     *
     * @return
     */
    @Override
    public List<History> findAll() {
        return historyMapper.selectAll();
    }

    /**
     * 根据用户名查询用户浏览历史
     * @param username
     * @return
     */
    @Override
    public List<Detail> findHistoryByUsername(String username) {


        List<Detail> historyVideoList = new ArrayList<>();

        History history = new History();
        history.setUsername(username);

        //获取history中的aid并以最近优先排序
        List<String> aidList = historyMapper.findHistoryByUsername(username);
        for (String aid : aidList) {

            //通过aid查询视频对象
            Detail detail = detailFeign.findById(aid).getData();

            historyVideoList.add(detail);
        }

        return historyVideoList;
    }
}
